<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" name="system-online-user" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { getOnlineUserList, deleteToken } from './onlineuser.api';
  import { columns, searchFormSchema } from './onlineuser.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: 'online-user',
    tableProps: {
      //在线用户rowKey默认id会造成key重复，导致页面出现重复数据
      rowKey: 'id',
      title: '在线用户列表',
      api: getOnlineUserList,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '强退',
        popConfirm: {
          title: '是否强退用户(' + record.username + ')?',
          placement: 'left',
          confirm: handleForcedLogout.bind(null, record),
        },
      },
    ];
  }

  /**
   * 强退事件
   */
  async function handleForcedLogout(record) {
    await deleteToken(record.token, reload);
  }
</script>
